//
//  AppDelegate.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "AppDelegate.h"
#import "ChooseCarController.h"
#import "MineViewController.h"
#import "ActiveViewController.h"
#import "CarFriendViewController.h"
#import "MainNewsViewController.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"

@interface AppDelegate ()

@end


@implementation AppDelegate

- (void)dealloc
{
    [_scroll release];
    [_window release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    [self.window makeKeyAndVisible];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    
//    //设置微信AppId、appSecret，分享url
//    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        //[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
        _scroll.contentSize = CGSizeMake(self.window.frame.size.width * 4, self.window.frame.size.height);
        _scroll.bounces = NO;
        _scroll.pagingEnabled = YES;
        _scroll.showsHorizontalScrollIndicator = NO;
        for (int i = 1; i < 5; i++) {
            NSString *name = [NSString stringWithFormat:@"car%d", i];
            UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width * (i - 1), 0, self.window.frame.size.width, self.window.frame.size.height)];
            [iView setImage:[UIImage imageNamed:name]];
            [_scroll addSubview:iView];
            [iView release];
        }
        UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(self.window.frame.size.width * 3.5 - 50, self.window.frame.size.height - 130, 100, 40)];
        [b setTitle:@"立即进入" forState:UIControlStateNormal];
        [b addTarget:self action:@selector(b) forControlEvents:UIControlEventTouchUpInside];
        b.titleLabel.font = [UIFont boldSystemFontOfSize:19];
        b.layer.borderColor = [UIColor whiteColor].CGColor;
        b.layer.borderWidth = 2;
        [_scroll addSubview:b];
        [self.window addSubview:_scroll];
        [b release];
        [_scroll release];
    }
    else{
        [self loveCar];
    }
    return YES;
}
- (void)b
{
    [_scroll removeFromSuperview];
    [self loveCar];
}
- (void)loveCar
{
    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:UmengAppkey];
    
    MainNewsViewController *news = [[MainNewsViewController alloc] init];
    UINavigationController *navNews = [[UINavigationController alloc] initWithRootViewController:news];
    navNews.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"要闻" image:[UIImage imageNamed:@"news.png"] tag:1];
    [news release];
    
    CarFriendViewController *friend = [[CarFriendViewController alloc] init];
    UINavigationController *navFriend = [[UINavigationController alloc] initWithRootViewController:friend];
    navFriend.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"车友" image:[UIImage imageNamed:@"message.png"] tag:2];
    [friend release];
    
    ChooseCarController *choose = [[ChooseCarController alloc] init];
    UINavigationController *navChoose = [[UINavigationController alloc] initWithRootViewController:choose];
    [choose release];
    choose.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"选车" image:[UIImage imageNamed:@"car.png"] tag:3];
    
    ActiveViewController *active = [[ActiveViewController alloc] init];
    UINavigationController *navActive = [[UINavigationController alloc] initWithRootViewController:active];
    navActive.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"降价" image:[UIImage imageNamed:@"money.png"] tag:4];
    [active release];
    
    MineViewController *mine = [[MineViewController alloc] init];
    UINavigationController *navMine = [[UINavigationController alloc] initWithRootViewController:mine];
    navMine.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"person.png"] tag:5];
    [mine release];
    
    UITabBarController *tabBra = [[UITabBarController alloc] init];
    tabBra.viewControllers = @[navNews, navFriend, navChoose, navActive, navMine];
    [self.window setRootViewController:tabBra];
    
    [navNews release];
    [navFriend release];
    [navChoose release];
    [navActive release];
    [navMine release];
    [tabBra release];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
